package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.EquipModel;
import com.app.service.AdminUserService;
import com.app.service.CategoryService;
import com.app.service.EquipService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CategoryVo;
import com.app.vo.EquipVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 车辆信息管理
 */
@RestController
@Controller
@RequestMapping("/api/equip")
public class EquipController {
    @Autowired
    private EquipService equipService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getDropList")//下拉框查询
    public Result<Object> getDropList(
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CategoryVo vo = new CategoryVo();
        List<CategoryModel> categoryModelList = categoryService.getCategoryByPid(135);
        vo.setTypeList(categoryModelList);//型号类型
        List<CategoryModel> categoryModelList1 = categoryService.getCategoryByPid(134);
        vo.setStatusList(categoryModelList1);
        return Result.success(vo);
    }

    //新增和编辑
    @PostMapping("/insertEquip")
    public Result<Object> insertEquip(@RequestBody EquipModel equipModel) {
        if (equipModel.getId() > 0) {
            equipModel.setUpdateTime(CommonHelp.getNowTimestamp());
            equipService.update(equipModel);
        } else {
            equipModel.setCreateTime(CommonHelp.getNowTimestamp());
            equipService.insert(equipModel);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteEquip")
    public Result<Object> deleteEquip(Integer id) {
        equipService.delete(id);
        return Result.success("操作成功");
    }

    //查询列表
    @GetMapping("/selectEquip")
    public Result<Object> selectEquip(Integer page, Integer limit, String equipNumber,Integer sortType) {
        EquipVo vo = new EquipVo();
        Page<EquipModel> taskModelPage = equipService.selectCarList(page, limit, equipNumber,sortType);
        List<EquipModel> records = taskModelPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            CategoryModel categoryById = categoryService.getCategoryById(records.get(i).getType());
            records.get(i).setTypeText(categoryById.getName());
            CategoryModel categoryById1 = categoryService.getCategoryById(records.get(i).getStatus());
            records.get(i).setStatusText(categoryById1.getName());
        }
        int total = (int) taskModelPage.getTotal();
        vo.setList(records);
        vo.setCount(total);
        return Result.success(vo);
    }
}
